<?php
namespace app\appoint\model;
use app\BaseModel;
use think\facade\Db;

class IndexRecord extends BaseModel
{
    //定义表名
    protected $name = 'lb_appoint_record';

    /**
     * @var array
     * 查询器
     */
    protected  $append = [
        'sever_time_text',
        'staff_text',
        'user_text',
        'over_time_text'
    ];

    /**
     * @param $value
     * @param $data
     * @return string
     * 过期友好时间
     */
    public function getOverTimeTextAttr($value,$data){
        $over = lbGetDates($data['end_time']-time());
        return $over;
    }

    /**
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getStaffTextAttr($value,$data){
        $staff = Db::name('longbing_card_user_info')->where(['fans_id'=>$data['to_uid']])->field('id,phone,avatar')->find();
        $staff = transImagesOne($staff,['avatar']);
        return $staff;
    }
    /**
     * @param $value
     * @param $data
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取用户信息
     */
    public function getUserTextAttr($value,$data){
        $staff = Db::name('longbing_card_user')->where(['id'=>$data['user_id']])->field('id,nickName,avatarUrl')->find();
        return $staff;
    }
    /**
     * @param $value
     * @param $data
     * @return mixed
     * 服务时间
     */
    public function getSeverTimeTextAttr($value,$data){
        $time =  date('Y-m-d H:i', $data['start_time']) . '-' . date('H:i', $data['end_time']);
        return $time;
    }

    /**
     * @param $value
     * @return false|string
     * 转换时间
     */
    public function getCreateTimeAttr($value){
        return date('Y-m-d H:i:s',$value);
    }
//    /**
//     * @param $value
//     * 获取状态
//     * @return $list
//     */
//    public function getStatusAttr($value){
//        switch ($value) {
//            case 1:
//                $list = '未服务';
//                break;
//            case 2:
//                $list = '已完成';
//                break;
//            case 3:
//                $list = '已过期';
//                break;
//            default :
//                $list = '已取消';
//                break;
//        }
//        return $list;
//    }

    /**
     * @param $value
     * @return mixed|string
     * 转换图片
     */
    public function getProjectCoverAttr($value){
        $cover = transImagesOne([$value],['cover']);
        return is_array($cover)?$cover[0]:'';
    }

    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 预记录列表
     */
    public function recordList($dis){
        $data = $this
            ->alias('a')
            ->leftJoin('lb_appoint_project b','a.project_id=b.id')
            ->leftJoin('lb_appoint_classify c','c.id=b.classify_id')
            ->where($dis)
            ->field('a.*,b.title as project_title,b.cover as project_cover,b.appoint_price,b.desc,c.title as cate_title')
            ->order('a.id desc')
            ->paginate(10)
            ->toArray();

        return $data;
    }
    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 预记录列表
     */
    public function recordProInfo($dis){
        $data = $this
            ->alias('a')
            ->leftJoin('lb_appoint_project b','a.project_id=b.id')
            ->leftJoin('lb_appoint_classify c','c.id=b.classify_id')
            ->where($dis)
            ->field('a.*,b.title as project_title,b.cover as project_cover,b.appoint_price,b.desc,c.title as cate_title')
            ->find();
        return !empty($data)?$data->toArray():$data;
    }
    /**
     * @param $dis
     * @return int
     * 预约条数
     */
    public function recordCount($dis){
        $count = $this->where($dis)->count();
        return $count;
    }


    /**
     * @param $dis
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 预约详情
     */
    public function recordInfo($dis){
        $data = $this->where($dis)->find();
        return $data;

    }

    /**
     * @param $data
     * @return int|mixed|string
     * 添加预约
     */
    public function recordAdd($data){
        $data['create_time'] = time();
        $data['update_time'] = time();
        $res = $this->insert($data);
        $res = $this->getLastInsID();
        return $res;
    }

    /**
     * @param $dis
     * @param $data
     * @return IndexRecord
     * 编辑预约
     */
    public function recordUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;

    }

    /**
     * @param $uniacid
     * @return IndexRecord
     * 过期处理
     */
    public function changeStatus($uniacid){
        $dis[] = ['end_time','<',time()];
        $dis[] = ['uniacid','=',$uniacid];
        $dis[] = ['status','=',1];
        $res = $this->recordUpdate($dis,['status'=>3]);
        return $res;
    }




}